
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for error.
	''' </summary>
	Public Partial Class [error]
		Inherits BaseWebPage
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' this.pageTemplate.PageTitle = PageTitles.ERROR_PAGE;
			' Put user code to initialize the page here
			'lblSendFeedback.Text = "<a href='" + Constants.CONTACT_2BMATES + "?"+ Constants.SUBJECT_PARAM + "=" + Constants.FEEDBACK_ERROR_SUBJECT + "' target='_blank'>" + "Click here" + "</a>" + " to send a feedback." ;
			Me.initPageTitles(Constants.ERROR_PAGE, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			lblSendFeedback.Text = ""
			Dim ex As Exception = DirectCast(Session(Constants.ssnLastError), Exception)
			Session("LastError") = Nothing
			litWelcomeMsg.Text = ex.Message

			trErrorMsgBackground.Visible = True
			trBackButton.Visible = False

			Dim [error] As Integer = Convert.ToInt32(Request.Params("e"))
			Select Case [error]
				Case Constants.[ERROR]
					litWelcomeMsg.Text = Constants.ERROR_MESSAGE_GENERAL_ERROR
					Exit Select
				Case Constants.ERROR_NO_DIRECT_FRIEND
					litWelcomeMsg.Text = Constants.ERROR_MESSAGE_NOT_FRIEND
					lblTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_NoteTitle_TEXT")
					lblSendFeedback.Visible = False
					Exit Select
				Case Constants.ERROR_MISSING_PARAMETERS
					litWelcomeMsg.Text = Constants.ERROR_MESSAGE_GENERAL_ERROR
					Exit Select
				Case Else
					Select Case ex.[GetType]().Name
						Case "HttpRequestValidationException"
							litWelcomeMsg.Text = Constants.ERROR_MESSAGE_GENERAL_ERROR
							Exit Select
						Case "Exception"
							If ex.Message = Constants.ERROR_MESSAGE_ONLY_MEMBERS_ACCESS OrElse ex.Message = Constants.ERROR_MESSAGE_ONLY_MEMBER_CAN_VIEW_MESSAGES OrElse ex.Message = Constants.ERROR_MESSAGE_BLOG_ONLY_MEMBERS_ACCESS Then
								trErrorMsgBackground.Visible = False
								trBackButton.Visible = True
							End If

							Exit Select
						Case Else
							litWelcomeMsg.Text = Constants.ERROR_MESSAGE_GENERAL_ERROR
							Exit Select
					End Select
					Exit Select
			End Select

			SaveErrorLog(ex, Me)
		End Sub

		Public Shared Sub SaveErrorLog(ByVal ex As Exception, ByVal page As BaseWebPage)
			Dim sFileName As String = ""
			Dim sFilePath As String = ""
			Dim sFileContents As String = ""

			Try
				Dim dtToday As DateTime = DateTime.Today
				Dim dtNow As DateTime = DateTime.Now
				If Directory.Exists(page.Server.MapPath(".") + "\..\err_logs\") = True Then
					sFileName = "2bmates_err_log_" + dtToday.ToString("dd_MM_yyyy") + ".txt"
					sFilePath = page.Server.MapPath(".") + "\..\err_logs\" + sFileName
				Else
					Directory.CreateDirectory(page.Server.MapPath(".") + "\..\err_logs")
					sFileName = "2bmates_err_log_" + dtToday.ToString("dd_MM_yyyy") + ".txt"
					sFilePath = page.Server.MapPath(".") + "\..\err_logs\" + sFileName
				End If
				sFileContents = ""

				'ToString("F") + "\r\n" 
				sFileContents += "Date and Time: " + dtToday.ToLongDateString() + " " + dtNow.ToLongTimeString() + "" & Chr(13) & "" & Chr(10) & "" + "Error Message: " + ex.Message + "" & Chr(13) & "" & Chr(10) & "" + "Error Source: " + ex.Source + "" & Chr(13) & "" & Chr(10) & "" + "Error InnerException: " + ex.InnerException + "" & Chr(13) & "" & Chr(10) & "" + "Error TargetSite : " + ex.TargetSite + "" & Chr(13) & "" & Chr(10) & "" + "Stack Trace: " + ex.StackTrace + "" & Chr(13) & "" & Chr(10) & "" + "Exception Type: " + ex.[GetType]().Name + "" & Chr(13) & "" & Chr(10) & "" + "URL source: " + page.Page.Request.Params("SCRIPT_NAME") + "" & Chr(13) & "" & Chr(10) & "" + "Browser: " + page.Page.Request.Params("HTTP_USER_AGENT") + "" & Chr(13) & "" & Chr(10) & "" + "HTTP_CONNECTION: " + page.Page.Request.Params("HTTP_CONNECTION") + "" & Chr(13) & "" & Chr(10) & "" + "HTTP_KEEP_ALIVE: " + page.Page.Request.Params("HTTP_KEEP_ALIVE") + "" & Chr(13) & "" & Chr(10) & ""

				If page.LoggedUser <> Nothing Then
					sFileContents += "User : #" + page.LoggedUser.ID + ", " + page.LoggedUser.Name + " " + page.LoggedUser.LastName + "" & Chr(13) & "" & Chr(10) & ""
				Else
					sFileContents += "User : #-1  Anonimous " & Chr(13) & "" & Chr(10) & ""
				End If
				sFileContents += "*********************************************************************" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & ""

				Dim fSR As StreamWriter = File.AppendText(sFilePath)
				fSR.Write(sFileContents)
				fSR.Flush()
				fSR.Close()

			Catch
			End Try
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






